<template>
  <div class="login-bg">
    <div class="login-top">
      <img src="@/assets/login/loginTop.png" class="login-img" />
    </div>
    <div class="login-card">
      <div class="login-title">福田资产管理系统</div>
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          left-icon="user-o"
          placeholder="Mr.green"
          class="login-field"
        />
        <van-field
          v-model="password"
          :type="isShowPwd ? 'text' : 'password'"
          left-icon="lock"
          @click-right-icon="rightClick"
          :right-icon="isShowPwd ? 'eye-o' : 'closed-eye'"
          placeholder="************"
          class="login-field"
        />
        <div style="margin: 24px 0 0 0">
          <van-button round block type="primary" native-type="submit">登录</van-button>
        </div>
      </van-form>
    </div>
    <div class="login-footer">版权所有<br />XXXXXXXXXXXXXXX</div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { useUserStore } from '@/stores/user'
const userStore = useUserStore()
const username = ref('admin')
const password = ref('123456')
const router = useRouter()

const onSubmit = () => {
  if (username.value === 'admin' && password.value === '123456') {
    localStorage.setItem('token', 'mock-token')
    showToast('登录成功')
    const userInfo = {
      name: '张三1',
      role: 'admin',
      id: '22434',
    }
    userStore.setUserInfo(userInfo) // 新增：存到pinia
    router.push('/')
  } else {
    showToast('用户名或密码错误')
  }
}
const isShowPwd = ref(false)
function rightClick() {
  console.log(111)
  isShowPwd.value = !isShowPwd.value
}
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  background: linear-gradient(180deg, #3a7afe 0%, #6eb6ff 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
}

.login-top {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-top: 46px;
  position: relative;
  z-index: 100;
}

.login-img {
  width: 290px;
  height: auto;
}

.login-card {
  background: #fff;
  border-radius: 32px 32px 0 0;
  box-shadow: 0 4px 24px rgba(58, 122, 254, 0.08);
  padding: 32px 0 24px 0;
  width: 100%;
  margin-top: -24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  /* 新增：让卡片填满剩余空间 */
  min-height: 0;
  /* 防止溢出 */
}

.login-title {
  font-size: 20px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 24px;
  color: #222;
}

.login-field {
  margin-bottom: 16px;
}

.login-footer {
  position: absolute;
  bottom: 24px;
  width: 100%;
  text-align: center;
  color: #ccc;
  font-size: 12px;
  line-height: 1.5;
  letter-spacing: 1px;
}
</style>
